import { defineStore } from "pinia";
import { cloneDeep } from "lodash-es";
import { cartAdd, cartGet } from "../api/user";
import { useUserStore } from "./user";
export interface CartType {
  userid: string;
  objectId: string;
  proname: string;
  originprice: number;
  img1: string;
  ischeck: boolean;
  n: number;
}
export interface CartState {
  list: CartType[];
}
export const useCart = defineStore("cart", {
  state: (): CartState => ({
    list: [
      // {
      //   objectId: "1212",
      //   proname: "手机",
      //   originprice: 999,
      //   img1: "",
      //   ischeck: false,
      //   n: 2,
      // },
      // {
      //   objectId: "121234534",
      //   proname: "洗衣机",
      //   originprice: 9998,
      //   img1: "",
      //   ischeck: true,
      //   n: 1,
      // },
    ],
  }),
  getters: {
    cartAllInfo() {
      //统计购物车总计信息 总数、总价、全选
      let allNum = 0; //总数
      let checkPrice = 0; //应付总价
      let allCheck = true; //全选状态,假设默认全选
      this.list.forEach((item: CartType) => {
        allNum += item.n;
        if (item.ischeck) {
          checkPrice += item.n * item.originprice * 100;
        }
        if (!item.ischeck) {
          allCheck = false;
        }
      });

      return {
        allNum,
        checkPrice,
        allCheck,
      };
    },
  },
  actions: {
    async cartAddAction(cartObj: CartType) {
      console.log(cartObj);
      // this.list.push(cartObj);  // 这种语法，无法触发list响应式变化
      for (let i = 0; i < this.list.length; i++) {
        //判断是否已经存在相同商品
        if (this.list[i].objectId === cartObj.objectId) {
          this.list[i].n++;
          // localStorage.setItem("cartList", JSON.stringify(this.list));
          return;
        }
      }
      //新增一条
      const user = useUserStore();
      let userid = user?.userInfo?.objectId;
      if (userid) {
        // cartObj.userid = userid; //为购物车数据追加用户id
        let { proname, img1, originprice, ischeck, n } = cartObj;
        let res = await cartAdd({
          proname,
          img1,
          originprice,
          ischeck,
          n,
          userid,
        });
        console.log(res);
        this.list = [...this.list, cartObj]; //只有让list后面的数组对象整体发生变化才能触发响应式
        // localStorage.setItem("cartList", JSON.stringify(this.list));
      }
      // console.log(this.list);
    },
    async cartInitAction() {
      //初始化购物车数据
      const user = useUserStore();
      let userid = user?.userInfo?.objectId;
      if (userid) {
        let res = await cartGet(userid);
        console.log(res);
        this.list = res.data.results;
      }
    },
    cartAllCheckAction() {
      //全选按钮
      //得到原来的全选状态
      // console.log(this.cartAllInfo.allCheck);
      // let arr = [...this.list];
      // console.log(arr[0] === this.list[0]);  //浅拷贝,true
      let arr = cloneDeep(this.list);
      // console.log(arr[0] === this.list[0]);  //深拷贝,false
      arr.forEach((item: CartType) => {
        item.ischeck = this.cartAllInfo.allCheck;
      });
      this.list = arr;
    },
  },
});
